package com.jch.demo.intro.agent;

import java.lang.reflect.InvocationHandler;
import java.lang.reflect.Method;
import java.lang.reflect.Proxy;

/**
 * 代理模式 实现验证
 */
public class CalculatorValidationHandler implements InvocationHandler {

    private Object target;

    public CalculatorValidationHandler(Object target) {
        this.target = target;
    }

    public static Object createProxy(Object target){
        return Proxy.newProxyInstance(target.getClass().getClassLoader(),target.getClass().getInterfaces(),new CalculatorValidationHandler(target));
    }

    private void validate(double a){
        if (a < 0){
            throw new IllegalArgumentException("Positive numbers only");
        }
    }

    @Override
    public Object invoke(Object proxy, Method method, Object[] args) throws Throwable {
        for (Object arg:args) {
            validate((Double) arg);
        }
        Object result = method.invoke(target,args);
        return result;
    }
}
